package com.lekang.system.service.impl;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Date;
import com.lekang.common.utils.DateUtils;
import com.lekang.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lekang.system.mapper.AppointmentRegistrationMapper;
import com.lekang.system.domain.AppointmentRegistration;
import com.lekang.system.service.IAppointmentRegistrationService;

/**
 * 预约登记Service业务层处理
 * 
 * @author 刘卓
 * @date 2025-10-13
 */
@Service
public class AppointmentRegistrationServiceImpl implements IAppointmentRegistrationService 
{
    @Autowired
    private AppointmentRegistrationMapper appointmentRegistrationMapper;

    /**
     * 查询预约登记
     * 
     * @param id 预约登记主键
     * @return 预约登记
     */
    @Override
    public AppointmentRegistration selectAppointmentRegistrationById(Long id)
    {
        return appointmentRegistrationMapper.selectAppointmentRegistrationById(id);
    }

    /**
     * 查询预约登记列表
     * 
     * @param appointmentRegistration 预约登记
     * @return 预约登记
     */
    @Override
    public List<AppointmentRegistration> selectAppointmentRegistrationList(AppointmentRegistration appointmentRegistration)
    {
        return appointmentRegistrationMapper.selectAppointmentRegistrationList(appointmentRegistration);
    }

    /**
     * 新增预约登记
     * 
     * @param appointmentRegistration 预约登记
     * @return 结果
     */
    @Override
    public int insertAppointmentRegistration(AppointmentRegistration appointmentRegistration)
    {
        // 数据验证
        String validateResult = validateAppointment(appointmentRegistration);
        if (StringUtils.isNotEmpty(validateResult)) {
            throw new RuntimeException(validateResult);
        }
        
        // 设置默认状态为待上门
        appointmentRegistration.setStatus(AppointmentRegistration.STATUS_PENDING);
        appointmentRegistration.setCreateTime(DateUtils.getNowDate());
        appointmentRegistration.setIsDeleted(0L);
        
        return appointmentRegistrationMapper.insertAppointmentRegistration(appointmentRegistration);
    }

    /**
     * 修改预约登记
     * 
     * @param appointmentRegistration 预约登记
     * @return 结果
     */
    @Override
    public int updateAppointmentRegistration(AppointmentRegistration appointmentRegistration)
    {
        return appointmentRegistrationMapper.updateAppointmentRegistration(appointmentRegistration);
    }

    /**
     * 批量删除预约登记
     * 
     * @param ids 需要删除的预约登记主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentRegistrationByIds(Long[] ids)
    {
        return appointmentRegistrationMapper.deleteAppointmentRegistrationByIds(ids);
    }

    /**
     * 删除预约登记信息
     * 
     * @param id 预约登记主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentRegistrationById(Long id)
    {
        return appointmentRegistrationMapper.deleteAppointmentRegistrationById(id);
    }

    /**
     * 到院核销：仅待上门可置为已完成
     */
    @Override
    public int markCompleted(Long id, java.util.Date arrivalTime)
    {
        return appointmentRegistrationMapper.markCompleted(id, arrivalTime);
    }

    /**
     * 取消预约：仅待上门可取消
     */
    @Override
    public int cancelById(Long id)
    {
        return appointmentRegistrationMapper.cancelById(id);
    }

    /**
     * 定时任务：超过1小时未到院置为已过期
     */
    @Override
    public int expireOvertime()
    {
        return appointmentRegistrationMapper.expireOvertime();
    }

    /**
     * 获取预约统计信息
     */
    @Override
    public Map<String, Object> getAppointmentStatistics()
    {
        return appointmentRegistrationMapper.getAppointmentStatistics();
    }

    /**
     * 验证预约数据
     */
    @Override
    public String validateAppointment(AppointmentRegistration appointmentRegistration)
    {
        if (appointmentRegistration == null) {
            return "预约信息不能为空";
        }
        
        if (StringUtils.isEmpty(appointmentRegistration.getApplicantName())) {
            return "预约人姓名不能为空";
        }
        
        if (StringUtils.isEmpty(appointmentRegistration.getPhone())) {
            return "预约人手机号不能为空";
        }
        
        // 验证手机号格式
        if (!appointmentRegistration.getPhone().matches("^1[3-9]\\d{9}$")) {
            return "手机号格式不正确";
        }
        
        if (appointmentRegistration.getAppointmentTime() == null) {
            return "预约时间不能为空";
        }
        
        // 验证预约时间不能是过去时间
        if (appointmentRegistration.getAppointmentTime().before(new Date())) {
            return "预约时间不能是过去时间";
        }
        
        // 验证预约时间不能超过30天
        Date maxDate = new Date(System.currentTimeMillis() + 30L * 24 * 60 * 60 * 1000);
        if (appointmentRegistration.getAppointmentTime().after(maxDate)) {
            return "预约时间不能超过30天";
        }
        
        if (appointmentRegistration.getElderlyId() == null) {
            return "老人信息不能为空";
        }
        
        return null;
    }

    /**
     * 根据状态查询预约列表
     */
    @Override
    public List<AppointmentRegistration> selectAppointmentRegistrationListByStatus(String status)
    {
        AppointmentRegistration queryParam = new AppointmentRegistration();
        queryParam.setStatus(status);
        return appointmentRegistrationMapper.selectAppointmentRegistrationList(queryParam);
    }
}
